//
//  NetManager.m
//  HYSmileiOS
//
//  Created by alekye on 16/4/26.
//  Copyright © 2016年 heyou. All rights reserved.
//

#import "NetManager.h"
#import "heyou.h"

@interface NetManager () {
    AFHTTPSessionManager *httpManager;
}

@end

@implementation NetManager
/**
 *  网络管理器，封装AFHTTPSessionManager，
 *  单例可以增加网络访问速度
 *
 *  @return 单实例
 */
+ (instancetype)sharedInstance {
    static NetManager *netInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        netInstance = [[self alloc] init];
    });
    return netInstance;
}

/**
 *  构造函数
 *
 *  @return NetManager
 */
- (instancetype)init {
    self = [super init];
    if (self) {
        httpManager = [AFHTTPSessionManager manager];
    }
    return self;
}

/**
 *  使用GET方法获取网络数据
 *
 *  @param URLString  URL地址
 *  @param parameters 参数
 *  @param success    成功回调
 *  @param failure    失败回调
 */
- (void)GET:(NSString *)URLString
 parameters:(NSDictionary*)parameters
    success:(void (^)(NSDictionary* responseDic))success
    failure:(void (^)(NSError *error))failure {
    
    [httpManager GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

@end
